﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class AttachmentDao
    {
        public bool Delete(SqlConnection conn, string attachmentId)
        {
            string commandText = "DELETE FROM [dbo].[Attachment] WHERE [AttachmentId] = @AttachmentId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@attachmentId", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = attachmentId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public bool DeleteSingleAttach(SqlConnection conn, string attachmentId, string fileName)
        {
            string commandText = "DELETE FROM [dbo].[Attachment] WHERE [AttachmentId] = @AttachmentId AND [Name] = @Name";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50);
            commandParameters[0].Value = attachmentId;
            commandParameters[1] = new SqlParameter("@Name", SqlDbType.NVarChar, 100);
            commandParameters[1].Value = fileName;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public bool Insert(SqlConnection conn, AttachmentData data)
        {
            string commandText = "INSERT [dbo].[Attachment] ( [Type], [AttachmentId], [Name], [AddTime], [TotalSize], [UserId] ) VALUES ( @Type, @AttachmentId, @Name, @AddTime, @TotalSize, @UserId )";
            SqlParameter[] commandParameters = new SqlParameter[6];
            commandParameters[0] = new SqlParameter("@Type", SqlDbType.Int);
            commandParameters[0].Value = data.Type;
            commandParameters[1] = new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = data.AttachmentId;
            commandParameters[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 100);
            commandParameters[2].Value = data.Name;
            commandParameters[3] = new SqlParameter("@AddTime", SqlDbType.DateTime);
            commandParameters[3].Value = data.AddTime;
            commandParameters[4] = new SqlParameter("@TotalSize", SqlDbType.Int);
            commandParameters[4].Value = data.TotalSize;
            commandParameters[5] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[5].Value = data.UserId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public DataTable SelectByAttachmentId(SqlConnection conn, string attachmentId)
        {
            string commandText = "SELECT [AttachId], [Type], [AttachmentId], [Name], [UserId], [AddTime], [TotalSize], [DeleteFlag] FROM [dbo].[Attachment] WHERE [AttachmentId] = @AttachmentId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50) };
            commandParameters[0].Value = attachmentId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Attachment";
            return set.Tables[0];
        }
    }
}

